package com.jph.simple;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.annotation.Nullable;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import adapter.FriendAdapter;
import container.ChatManager;
import container.FriendManager;
import model.Friend;

import static android.content.Context.ACTIVITY_SERVICE;


/**
 * A simple {@link Fragment} subclass.
 */


public class FriendFragment extends Fragment {

    public static final String TAG = "FriendFragment";

    private ListView lvFriends;

    private List<Friend> mFriendList = new ArrayList<>();

    private static final int[] imageIds = { R.drawable.world, R.drawable.friend_1,
            R.drawable.friend_2, R.drawable.friend_3, R.drawable.friend_4, R.drawable.friend_5,
            R.drawable.friend_6, R.drawable.friend_7, R.drawable.friend_8, R.drawable.friend_9,
    };

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

//        ((MainActivity)context).addHandler(TAG, mHandler);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_friend, container, false);

        lvFriends = (ListView) v.findViewById(R.id.lvFriends);
        updateFriendList();

        FriendAdapter friendAdapter = new FriendAdapter(getActivity(),
                R.layout.listview_item, mFriendList);          //关联数据和子布局
        lvFriends.setAdapter(friendAdapter);                   //绑定数据和适配器
        lvFriends.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                Friend friend= mFriendList.get(position);         //获取点击的那一行

                ChatManager.getInstance().isHomeReload = true;

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(PlayerChatActivity.ParamName, friend.getName());
                bundle.putInt(PlayerChatActivity.ParamType, PlayerChatActivity.ChatTypePlayer);

                if (friend.getName().equals(getResources().getString(R.string.group_chat))){
                    intent.setClass(getActivity(), GroupActivity.class);
                    getActivity().finish();
                } else {
                    intent.setClass(getActivity(), PlayerChatActivity.class);
                }

                intent.putExtras(bundle);

                startActivity(intent);
                getActivity().finish();
            }
        });
        checkNewMessage();

        return v;
    }

    //判断界面是否在顶层
    private boolean isTopActivity() {

        boolean isTop = false;
        ActivityManager am = (ActivityManager)getActivity().getSystemService(ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        if (cn.getClassName().contains(TAG))// TAG为Activity的全称
        {
            isTop = true;
        }
        return isTop;
    }

    public void checkNewMessage(){

        updateFriendList();

        for (Friend friend : mFriendList){
            ArrayList<String> chatList = ChatManager.getInstance().getChat(friend.getName());
            if (chatList != null && !chatList.isEmpty()) {

                friend.setNewMessage(true);
                ((FriendAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
            }
        }
    }

//    private void updateOnline(String name, boolean isOnline){
//
//        if (name == null) return;
//
//        updateFriendList();
//
////        boolean isInList = false;
////
////        for (Friend friend : mFriendList){
////            if (friend.getName().equals(name)){
//////                if (friend.getOnline() == isOnline) break;
////                friend.setOnline(isOnline);
////                isInList = true;
////                break;
////            }
////        }
////        if (!isInList){
////            mFriendList.add(new Friend(imageIds[imageIds.length - 1], name, true));
////        }
////
////        //重新排序
////        Collections.sort(mFriendList, new SortFriendByOnline());
//
////        ((FriendAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
//    }

//    public Handler getHandler() {return mHandler;}

    public void updateUI(){
        updateFriendList();
        ((FriendAdapter) lvFriends.getAdapter()).notifyDataSetChanged();
    }

    private void updateFriendList(){

        mFriendList.clear();

        mFriendList.addAll(FriendManager.getInstance().getFriendList());
        Collections.sort(mFriendList, new SortFriendByOnline());

        mFriendList.add(0, new Friend(Friend.imageIds[0], getResources().getString(R.string.group_chat), true));

    }
}